<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title></title>
    <script src="js/mui.min.js"></script>
    <link href="css/mui.min.css" rel="stylesheet"/>
</head>
<body>
	<div>
		请输入服务器地址：
		<input type="text" value="http://192.168.0.107:8080/" id="ip"/>
		<button id="musicButton" onclick="begin('music')">开始备份音乐</button>
		<button id="picButton" onclick="begin('pic')">开始备份图片</button>
		<button id="videoButton" onclick="begin('video')">开始备份视频</button>
		<button id="backButton" onclick="back()">备份后台运行</button>
		<button id="viewMedia" onclick="viewMedia()">浏览资源</button>
	</div>
	<div><ul id="list" name="list"></ul></div>
</body>
<script type="text/javascript">  
            mui.init();  
			var ready = 0;
			document.addEventListener("plusready",function(){ready = 1;}, false); 
			var success = 0;
			var beg = 0;
			var ip = null;
			
			function viewMedia(){
				if(ready == 1){
					ip = document.getElementById("ip").value;
					if(ip){
						plus.runtime.openURL( ip+"media/", function(){
							alert("打开浏览器失败");
						}, "com.android.browser" );
						}
					else{alert('请输入ip地址');}
				}
				else{
					alert('系统还没准备好，请稍后重试');
				}
			}
			
			function back(){
				    var main = plus.android.runtimeMainActivity();  
				    main.moveTaskToBack(true);  
			}
			
			function begin(type){
				if(ready == 1){
					ip = document.getElementById("ip").value;
					if(ip){
						beg = 0;
						upPre(type,check);
						}
					else{alert('请输入ip地址');}
				}
				else{
					alert('系统还没准备好，请稍后重试');
				}
			}
			
			function upPre(type,callback){
				if(plus.os.name == "Android") {
				    var Context = plus.android.importClass("android.content.Context");  
				    var ContentResolver = plus.android.importClass("android.content.ContentResolver");  
				    var Cursor = plus.android.importClass("android.database.Cursor");  
				    var Uri = plus.android.importClass("android.net.Uri");  
				    var MediaStore = plus.android.importClass("android.provider.MediaStore");  
				    var main = plus.android.runtimeMainActivity();  
				    var form = document.getElementById("list"); 
				    var context = main;  
				    var Uri = new Uri();  
					if(type=='music') Uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
					if(type=='pic') Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
					if(type=='video') Uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
				    var resolver = new ContentResolver();  
				    resolver = context.getContentResolver();  
				    var c = new Cursor();  
				    c =resolver.query(Uri, null, null, null, null);  
				    c.moveToFirst();  
					(callback && typeof(callback)==="function") && callback(type,c,MediaStore);
				}
			}
			
			function check(type,c,MediaStore){
				if((beg&&c.moveToNext())||(!beg&&c)) {
					beg = 1;
					plus.uploader.clear( 4 );
					if(type=='music'){
						var name = c.getString(c.getColumnIndex(MediaStore.Audio.Media.TITLE));
						var size = c.getString(c.getColumnIndex(MediaStore.Audio.Media.SIZE));
						var path = c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA)); 
					}
					if(type=='pic'){
						var name = c.getString(c.getColumnIndex(MediaStore.Images.Media.TITLE));
						var size = c.getString(c.getColumnIndex(MediaStore.Images.Media.SIZE));
						var path = c.getString(c.getColumnIndex(MediaStore.Images.Media.DATA)); 
					}
					if(type=='video'){
						var name = c.getString(c.getColumnIndex(MediaStore.Video.Media.TITLE)); 
						 var size = c.getString(c.getColumnIndex(MediaStore.Video.Media.SIZE));
						var path = c.getString(c.getColumnIndex(MediaStore.Video.Media.DATA)); 
					}
					var task = null;
					task = plus.uploader.createUpload(
						ip,
						{ method:"POST",priority:100},
						function(t,status){
							if(t.responseText == 'true'){
								upload(type,c,MediaStore,name,path);
							}
							else{
								var li = document.createElement('li');
								li.innerHTML = name+'已经存在';
								var form = document.getElementById('list');
								form.appendChild(li);
								check(type,c,MediaStore);
							}
						}
						);
					task.addData("name", name+"::"+type+"::"+size);
					task.start();
				}
				else{
					var li = document.createElement('li');
					li.innerHTML = type+'备份完成';
					var form = document.getElementById('list');
					form.appendChild(li);
				}
			}
			
			function upload(type,c,MediaStore,name,path){
					plus.uploader.clear( 4 );  
					var task = null;
					var li = document.createElement('li');
					li.innerHTML = '正在传输第'+(success+1)+'个'+type+':'+name;
					li.id = name;
					var form = document.getElementById('list');
					form.appendChild(li);
					console.log(path);
					task = plus.uploader.createUpload(
						ip,
						{ method:"POST",priority:100},
						function(){
							success+=1;
							var li = document.getElementById(name);
							li.innerHTML = '第'+success+'个'+type+'传输完毕：'+name;
							var form = document.getElementById('list');
							form.appendChild(li);
							console.log(success);
							check(type,c,MediaStore);}
						);
					task.addFile(path, {key:"file","name":name+"::"+type});
					task.start();
				}
        </script>
</html>